<?php

defined('BASEPATH') or die('forbidden');

/**
 * 家庭模块
 * @author  Karson
 * @name   family.php
 * @since   2013-2-27 23:22:55
 */
class Family_Model extends Model {

    public $familyinfo = array();
    public $familylist = array();

    function _init() {
        
    }

    function get_family_list($family_id = null) {
        $family_id = is_null($family_id) ? $this->auth->fields['family_id'] : $family_id;
        if (!$family_id)
            return array();
        if (isset($this->familylist[$family_id]))
            return $this->familylist[$family_id];
        $this->db->query("SELECT * FROM {pre}user u 
            LEFT JOIN {pre}user_extension ue ON u.id=ue.user_id 
            LEFT JOIN {pre}user_count uc ON u.id=uc.user_id 
            WHERE family_id='{$family_id}'");
        while ($row = $this->db->get_array()) {
            $this->familylist[$family_id][$row['id']] = $row;
        }
        return $this->familylist[$family_id];
    }

    function get_family_ids($family_id = null) {
        $family_list = $this->get_family_list($family_id);
        return array_keys($family_list);
    }

    function get_family($family_id = null) {
        $family_id = is_null($family_id) ? $this->auth->fields['family_id'] : $family_id;
        if (!$family_id)
            return array();
        if (isset($this->familyinfo[$family_id]))
            return $this->familyinfo[$family_id];
        $data = $this->db->one("SELECT * FROM {pre}family WHERE id='{$family_id}'");
        $this->familyinfo[$family_id] = $data;
        return $data;
    }

}